package com.hmyd.hmyd_android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.Message;
import com.hmyd.hmyd_android.utils.Utils;

public class MessageAdapter extends BaseAdapter {
	private Context mContext;
	private List<Message> lists;
	private LayoutInflater mInflater;
	public boolean isShow = false;
	public boolean isCheck = false;
	public onMsgSelectListener onMsgSelectListener;
	List<String> id = new ArrayList<String>();

	public interface onMsgSelectListener {
		void onMsgSelect(List<String> MsgId);
	}

	public MessageAdapter(Context context, List<Message> lists) {
		this.mContext = context;
		this.lists = lists;
		mInflater = LayoutInflater.from(context);
		onMsgSelectListener = (com.hmyd.hmyd_android.adapter.MessageAdapter.onMsgSelectListener) context;
	}

	@Override
	public int getCount() {
		return lists != null ? lists.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return lists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_message_list, null);
			holder = new ViewHolder();
			holder.tv_title = (TextView) convertView.findViewById(R.id.message_title);
			holder.tv_sendtime = (TextView) convertView.findViewById(R.id.message_time);
			holder.tv_content = (TextView) convertView.findViewById(R.id.message_content);
			holder.redView = (ImageView) convertView.findViewById(R.id.message_status_image);
			holder.line = convertView.findViewById(R.id.line);
			holder.cb_select = (CheckBox) convertView.findViewById(R.id.message_select);
			holder.rootLayout = (RelativeLayout) convertView.findViewById(R.id.message_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Message item = lists.get(position);
		holder.tv_title.setText(item.subject);
		holder.tv_sendtime.setText(Utils.getDateString(item.sendTime));
		if (isShow) {
			holder.rootLayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (holder.cb_select.isChecked()) {
						holder.cb_select.setChecked(false);
					} else {
						holder.cb_select.setChecked(true);
					}
				}
			});
		}
		holder.tv_content.setText(item.content);
		holder.cb_select.setChecked(isCheck);

		if (item.unread) {
			holder.redView.setBackgroundResource(R.drawable.xiao_icon_weid);
			holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.text_orange));
		} else {
			holder.redView.setBackgroundResource(R.drawable.xiao_icon_yid);
			holder.tv_title.setTextColor(mContext.getResources().getColor(
					R.color.recommendation_sidemenu_bg));
		}

		if (isShow) {
			holder.cb_select.setVisibility(View.VISIBLE);
		} else {
			holder.cb_select.setVisibility(View.GONE);
		}

		holder.cb_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					id.add(item.messageId);
					holder.cb_select.setVisibility(View.VISIBLE);
					onMsgSelectListener.onMsgSelect(id);
				}
			}
		});

		return convertView;

	}

	public class ViewHolder {
		public TextView tv_title;
		public TextView tv_sendtime;
		public TextView tv_content;
		public View line;
		public CheckBox cb_select;
		ImageView redView;
		public RelativeLayout rootLayout;

	}
}
